package com.dinian.demeter.controller;

import com.dinian.demeter.common.domain.ResponseBo;
import com.dinian.demeter.domain.Fault;
import com.dinian.demeter.domain.User;
import com.dinian.demeter.service.FaultService;
import com.dinian.demeter.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;


@Slf4j
@Controller
public class UserController {

    @Autowired
    private FaultService faultService;

    @Autowired
    private UserService userService;

    /**
     * 跳转登陆页
     * @return
     */
    @GetMapping("/login")
    public String Login(){
        return "login";
    }

    /**
     * 跳转首页
     * @return
     */
    @RequestMapping(value = {"/","/index"})
    public String index(){
        return "index";
    }

    /**
     * 注销
     * @return
     */
    @ResponseBody
    @GetMapping("/logout")
    public ResponseBo logout(){
        Subject subject = SecurityUtils.getSubject();
        if(subject!=null){
            subject.logout();
            log.info("logout success");
        }
        return ResponseBo.ok("注销成功");
    }

    /**
     * 修改密码
     * @param username
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @ResponseBody
    @RequestMapping("/updatePwd")
    public ResponseBo updatePwd(@RequestParam("username") String username,
                                @RequestParam("oldPwd") String oldPwd,
                                @RequestParam("newPwd") String newPwd){
        log.info("username: {},oldPwd: {},newPwd: {}",username,oldPwd,newPwd);
        Boolean updatePwd = userService.updatePwd(username, oldPwd, newPwd);
        if (updatePwd)
            return ResponseBo.ok("修改成功！");
        return ResponseBo.error("修改失败！");
    }

    /**
     * 用户登陆
     * @param username
     * @param password
     * @param remember
     * @param session
     * @return
     */
    @PostMapping("/login")
    public String loginUser(@RequestParam("username") String username,
                                @RequestParam("password") String password,
                                @RequestParam(defaultValue = "false") Boolean remember,
                                HttpSession session){
//        String md5pass = MD5Util.getMd5(password);
        log.info("username {}，remember {}",username,remember);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password,remember);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            log.info("login success username={}",token.getUsername());
            User user = (User)subject.getPrincipal();
            session.setAttribute("user",user);
            return "index";
        } catch (Exception e){
            return "login";
        }
    }



    @RequestMapping("/user")
    public ModelAndView initUser(ModelAndView mv, String userId){
        log.info("userId = {}",userId);
        List<Fault> faultList = faultService.findFaultByUserId(userId);
        log.info("faultList = {}",faultList);
        mv.addObject("faultList",faultList);
        mv.setViewName("user/user");
        return mv;
    }

    /**
     * 显示个人信息
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping("/showUser")
    public ResponseBo showUser(String username){
        if (StringUtils.isNotEmpty(username)){
            User showUser = userService.findUserByUsername(username);
            log.info("showUser success.username={}",showUser.getUsername());
            return ResponseBo.ok(showUser);
        }
        return ResponseBo.error("查询失败。");
    }
}
